package com.nhn.ssi.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;

import com.nhn.ssi.bo.TestBo;
import com.nhn.ssi.util.Pager;
import com.nhn.ssi.util.PagerService;
import com.opensymphony.xwork2.ActionSupport;

public class DBIndexGRTAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	TestBo testBo;
	String currentPage;
	String pagerMethod;
	String keyword;
	Pager pager;
	List<Object> list;
	List<Map<String, String>> retList;

	@SuppressWarnings("unchecked")
	public String execute() throws Exception {
		int totalRows = testBo.getUserListSize();
		PagerService pagerService = new PagerService();
		pager = pagerService.getPager(currentPage, pagerMethod, totalRows);
		this.setCurrentPage("" + pager.getCurrentPage());
		list = testBo.getUserList(pager);

		if ("".equals(keyword) || keyword == null) {
			return "input";
		}
		Hits hists = searchResultHits(keyword);

		return "searchResult";
	}

	public Hits searchResultHits(String keyword) throws IOException, ParseException {
		retList = new ArrayList<Map<String, String>>();
		Map<String, String> map = null;
		long startTime = new Date().getTime();
		Hits hits = null;
		String queryString = " (body:" + keyword + ")";
		Query query = null;
		IndexSearcher searcher = new IndexSearcher("c:\\index");

		Analyzer analyzer = new StandardAnalyzer();
		try {
			QueryParser qp = new QueryParser("body", analyzer);
			query = qp.parse(queryString);
		} catch (ParseException e) {
		}
		if (searcher != null) {
			hits = searcher.search(query);
			if (hits.length() > 0) {
				System.out.println("查找关键字【" + queryString + "】找到:" + hits.length() + " 个结果!");
				//				 测试一下索引的时间
				long endTime = new Date().getTime();
				System.out.println("这花费了" + (endTime - startTime) + "毫秒从索引中找到所要文档!");
				for (int k = 0; k < hits.length(); k++) {
					map = new HashMap<String, String>();
					Document doc = hits.doc(k);
					String filePath = doc.get("path");
					String title = doc.get("title");
					String img = doc.get("img");
					filePath = filePath.replace("\\", "/").replace("E:/download/website/360buy/", "http://");
					System.out.println("doc name " + filePath);
					System.out.println("title：" + title);
					System.out.println("img：" + img);

					map.put("path", filePath);
					map.put("title", title);
					map.put("img", img);
					retList.add(map);
				}

			} else {
				System.out.println("no result be 找到!");
			}
		}
		return hits;
	}

	public java.util.List<Object> getList() {
		return list;
	}

	public void setList(java.util.List<Object> list) {
		this.list = list;
	}

	public String getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(String currentPage) {
		this.currentPage = currentPage;
	}

	public TestBo getTestBo() {
		return testBo;
	}

	public void setTestBo(TestBo testBo) {
		this.testBo = testBo;
	}

	public String getPagerMethod() {
		return pagerMethod;
	}

	public void setPagerMethod(String pagerMethod) {
		this.pagerMethod = pagerMethod;
	}

	public Pager getPager() {
		return pager;
	}

	public void setPager(Pager pager) {
		this.pager = pager;
	}

	public List<Map<String, String>> getRetList() {
		return retList;
	}

	public void setRetList(List<Map<String, String>> retList) {
		this.retList = retList;
	}

	public String getKeyword() {
		return keyword;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}

}
